package com.coco.admin.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.coco.admin.mapper.AdminMapper;
import com.coco.admin.pojo.TbAdmin;
import com.coco.admin.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 增加
     */
    @Override
    public void add(TbAdmin admin) {
        adminMapper.insert(admin);
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            adminMapper.deleteByPrimaryKey(id);
        }
    }

    /**
     * 修改
     */
    @Override
    public void update(TbAdmin admin) {
        adminMapper.updateByPrimaryKey(admin);
    }


    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public TbAdmin findOne(Long id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbAdmin findName(String username) {
        TbAdmin name = adminMapper.findName(username);
        return name;
    }

    /**
     * 查询全部
     */
    @Override
    public List<TbAdmin> findAll() {
        return adminMapper.findAll();
    }

    @Override
    public String getToken(TbAdmin user) {
        String token = "";
        token += JWT.create().withAudience(user.getUserName())
                .sign(Algorithm.HMAC256(user.getPassword()));
        System.out.println("132" + token);
        return token;
    }
}

